package test.guava;

import com.google.common.util.concurrent.ListeningExecutorService;
import concurrent.ConcurrentUtil;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.Executors;

/**
 * 最简单的多线程使用方式：
 * 直接调用线程，调用后线程不驻留，无返回结果
 * @author shadowedge
 */
public class GuavaConcurrentExample {
	private static final Logger logger = LoggerFactory.getLogger(GuavaConcurrentExample.class);

	//使用默认名称、默认线程池(10)构建并发，运行结束后JVM会自动退出
	static ListeningExecutorService service = ConcurrentUtil.instance.getService();

    static ListeningExecutorService service2 = ConcurrentUtil.instance.getService("MY_WORKER_THREAD-%d",5,false);

	private void exam(){
		service.submit(() -> {
			logger.info("CALL future 1,{} !");
		});
	}

	private void exam2(){
        service2.submit(() -> {
			logger.info("CALL future 2!");
		});
	}

	@Test
	public void  test() throws InterruptedException {
		for (int i = 0; i < 10; i++) {
			exam();
		}

		for (int i = 0; i < 10; i++) {
			exam2();
		}
		//bad practice：避免直接使用Executors创建线程
		Executors.newSingleThreadExecutor();
		Thread.sleep(5000);
	}

//	public static void main(String[] args) throws InterruptedException {
//		GuavaConcurrentExample guavaConcurrentExample1 = new GuavaConcurrentExample();
//		GuavaConcurrentExample guavaConcurrentExample2 = new GuavaConcurrentExample();
//		guavaConcurrentExample1.exam();
//		guavaConcurrentExample2.exam();
//		logger.info("guavainst1:{}",guavaConcurrentExample1.toString());
//		logger.info("guavainst2:{}",guavaConcurrentExample2.toString());
//		Thread.sleep(5000);
//	}
}
